/*
 * @Author: your name
 * @Date: 2020-09-15 10:15:45
 * @LastEditTime: 2020-09-17 17:30:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense_demo\assets\scripts\loading_door.ts
 */

import { SoundMgr } from "./sound_mgr"

const {ccclass, property} = cc._decorator;

@ccclass
export default class LoadingDoor extends cc.Component {

    // 左右两扇门节点
    @property(cc.Node)
    private l_door: cc.Node = null;
    @property(cc.Node)
    private r_door: cc.Node = null;

    @property(cc.Integer)
    public door_state = 1;          // 0关门，1开门
    @property(cc.Float)
    public anim_duration = 0.1;

    onLoad() {}

    // 设置门的状态
    private _set_doot_state(state: number): void {
        this.door_state = state;
        
        let win_size: cc.Size = cc.winSize;
        if (this.door_state === 0) {    // 关门
            this.l_door.x = 0;
            this.r_door.x = 0;
        } else if (this.door_state === 1) {     // 开门
            this.l_door.x = -win_size.width * 0.5;
            this.r_door.x = win_size.width * 0.5;
        }
    }

    public set_door_state(state: number): void {
        if (this.door_state === state) {
            return;
        }

        this._set_doot_state(state);
    }

    // 关门动画
    public close_the_door(end_func: Function): void {
        if (this.door_state === 0) {
            return;
        }

        // let anim_com: cc.Animation = this.node.getComponent(cc.Animation);
        // anim_com.play("close_loading_door_anim");
        // this.scheduleOnce(()=>{
        //     if (end_func) {
        //         end_func();
        //     }
        // }, anim_com.currentClip.duration)
        
        let win_size: cc.Size = cc.winSize;
        this.door_state = 0;
        this.l_door.x = -win_size.width * 0.5;
        this.r_door.x = win_size.width * 0.5;
        
        cc.tween(this.l_door)
            .by(this.anim_duration, {position: cc.v3((win_size.width * 0.5 + 2), 0)})
            .start();

        cc.tween(this.r_door)
            .by(this.anim_duration, {position: cc.v3(-(win_size.width * 0.5 + 2), 0)})
            .call(()=>{
                // 播放关门音效
                SoundMgr.play_effect("sounds/close_door");
                if (end_func) {
                    end_func();
                }
            })
            .start();    
    }

    // 开门动画
    public open_the_door(end_func: Function): void {
        if (this.door_state === 1) {
            return;
        }

        // let anim_com: cc.Animation = this.node.getComponent(cc.Animation);
        // anim_com.play("open_loading_door_anim");
        // this.scheduleOnce(()=>{
        //     if (end_func) {
        //         end_func();
        //     }
        // }, anim_com.currentClip.duration)

        let win_size: cc.Size = cc.winSize;
        this.door_state = 1;
        this.l_door.x = 0;
        this.r_door.x = 0;

        cc.tween(this.l_door)
            .by(this.anim_duration, {position: cc.v3(-(win_size.width * 0.5 + 2), 0)})
            .start();

        cc.tween(this.r_door)
            .by(this.anim_duration, {position: cc.v3((win_size.width * 0.5 + 2), 0)})
            .call(()=>{
                if (end_func) {
                    end_func();
                }
            })
            .start();    
    }
}


